<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Flask | To Share</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/share/favicon.ico">
    <meta name="description" content="分享风雨，享受阳光">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/share/assets/css/0.styles.d7affcea.css" as="style"><link rel="preload" href="/share/assets/js/app.706684de.js" as="script"><link rel="preload" href="/share/assets/js/3.4685d505.js" as="script"><link rel="preload" href="/share/assets/js/1.b12738fb.js" as="script"><link rel="preload" href="/share/assets/js/67.fd9ba6df.js" as="script"><link rel="preload" href="/share/assets/js/9.4e81f08e.js" as="script"><link rel="prefetch" href="/share/assets/js/10.9e95ce7b.js"><link rel="prefetch" href="/share/assets/js/11.e99410dc.js"><link rel="prefetch" href="/share/assets/js/12.e9afe07c.js"><link rel="prefetch" href="/share/assets/js/13.078dd281.js"><link rel="prefetch" href="/share/assets/js/14.6575b8f0.js"><link rel="prefetch" href="/share/assets/js/15.d942bb10.js"><link rel="prefetch" href="/share/assets/js/16.fd8cc40c.js"><link rel="prefetch" href="/share/assets/js/17.f1b1f2c6.js"><link rel="prefetch" href="/share/assets/js/18.b312ce79.js"><link rel="prefetch" href="/share/assets/js/19.a99d170f.js"><link rel="prefetch" href="/share/assets/js/20.cda2b911.js"><link rel="prefetch" href="/share/assets/js/21.d6062607.js"><link rel="prefetch" href="/share/assets/js/22.5a50eee4.js"><link rel="prefetch" href="/share/assets/js/23.be601eed.js"><link rel="prefetch" href="/share/assets/js/24.3d0b9811.js"><link rel="prefetch" href="/share/assets/js/25.c1c35d96.js"><link rel="prefetch" href="/share/assets/js/26.9a8b03ff.js"><link rel="prefetch" href="/share/assets/js/27.6c302f3e.js"><link rel="prefetch" href="/share/assets/js/28.5eb55960.js"><link rel="prefetch" href="/share/assets/js/29.8297842b.js"><link rel="prefetch" href="/share/assets/js/30.b453256b.js"><link rel="prefetch" href="/share/assets/js/31.6ce2e46f.js"><link rel="prefetch" href="/share/assets/js/32.cf3906ab.js"><link rel="prefetch" href="/share/assets/js/33.83680f76.js"><link rel="prefetch" href="/share/assets/js/34.dada508c.js"><link rel="prefetch" href="/share/assets/js/35.11a36f17.js"><link rel="prefetch" href="/share/assets/js/36.fc43ef74.js"><link rel="prefetch" href="/share/assets/js/37.af715eba.js"><link rel="prefetch" href="/share/assets/js/38.7ba3e6e1.js"><link rel="prefetch" href="/share/assets/js/39.cd1c9d09.js"><link rel="prefetch" href="/share/assets/js/4.c1f99423.js"><link rel="prefetch" href="/share/assets/js/40.8df0c044.js"><link rel="prefetch" href="/share/assets/js/41.e643a418.js"><link rel="prefetch" href="/share/assets/js/42.d809cd4f.js"><link rel="prefetch" href="/share/assets/js/43.5136999f.js"><link rel="prefetch" href="/share/assets/js/44.315a82bf.js"><link rel="prefetch" href="/share/assets/js/45.a4b04306.js"><link rel="prefetch" href="/share/assets/js/46.2e8ec77e.js"><link rel="prefetch" href="/share/assets/js/47.d6bde2ca.js"><link rel="prefetch" href="/share/assets/js/48.e8e85c15.js"><link rel="prefetch" href="/share/assets/js/49.fce2ff19.js"><link rel="prefetch" href="/share/assets/js/5.599dfd12.js"><link rel="prefetch" href="/share/assets/js/50.3918e713.js"><link rel="prefetch" href="/share/assets/js/51.c5ddf50e.js"><link rel="prefetch" href="/share/assets/js/52.b6653f73.js"><link rel="prefetch" href="/share/assets/js/53.c0420367.js"><link rel="prefetch" href="/share/assets/js/54.0f64522f.js"><link rel="prefetch" href="/share/assets/js/55.1f9ce6fb.js"><link rel="prefetch" href="/share/assets/js/56.0a2fc30f.js"><link rel="prefetch" href="/share/assets/js/57.3b81efcb.js"><link rel="prefetch" href="/share/assets/js/58.c617dcad.js"><link rel="prefetch" href="/share/assets/js/59.5239e9d4.js"><link rel="prefetch" href="/share/assets/js/6.4e5676a9.js"><link rel="prefetch" href="/share/assets/js/60.b614d70b.js"><link rel="prefetch" href="/share/assets/js/61.f45be005.js"><link rel="prefetch" href="/share/assets/js/62.52121455.js"><link rel="prefetch" href="/share/assets/js/63.9f699597.js"><link rel="prefetch" href="/share/assets/js/64.01c4bf37.js"><link rel="prefetch" href="/share/assets/js/65.0f31fd4b.js"><link rel="prefetch" href="/share/assets/js/66.55dbbe80.js"><link rel="prefetch" href="/share/assets/js/68.c6c9e158.js"><link rel="prefetch" href="/share/assets/js/69.91757641.js"><link rel="prefetch" href="/share/assets/js/7.b8928c33.js"><link rel="prefetch" href="/share/assets/js/70.12f0a894.js"><link rel="prefetch" href="/share/assets/js/71.88c99794.js"><link rel="prefetch" href="/share/assets/js/72.e1d99c59.js"><link rel="prefetch" href="/share/assets/js/73.aee6f06a.js"><link rel="prefetch" href="/share/assets/js/74.b47532b0.js"><link rel="prefetch" href="/share/assets/js/75.1f1ab66e.js"><link rel="prefetch" href="/share/assets/js/8.56deb6eb.js">
    <link rel="stylesheet" href="/share/assets/css/0.styles.d7affcea.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>To Share</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>分享风雨，享受阳光</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><!---->
            
          <span data-v-25ba6db2>2018 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/share/" class="home-link router-link-active"><!----> <span class="site-name">To Share</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/share/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/categories/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/flex/" class="nav-link"><i class="undefined"></i>
  flex
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/gui/" class="nav-link"><i class="undefined"></i>
  gui
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/nodeJs/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/python/" class="nav-link"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li></ul></div></div><div class="nav-item"><a href="/share/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/share/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Docs
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/docs/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/flex/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/node/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/python/" class="nav-link router-link-active"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/gui/" class="nav-link"><i class="undefined"></i>
  GUI
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/zenglingchuan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><img src="/share/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <!----> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>63</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>51</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/share/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/categories/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/flex/" class="nav-link"><i class="undefined"></i>
  flex
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/gui/" class="nav-link"><i class="undefined"></i>
  gui
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/nodeJs/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/python/" class="nav-link"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li></ul></div></div><div class="nav-item"><a href="/share/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/share/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Docs
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/docs/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/flex/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/node/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/python/" class="nav-link router-link-active"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/gui/" class="nav-link"><i class="undefined"></i>
  GUI
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/zenglingchuan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <ul class="sidebar-links"><li><a href="/share/docs/python/" aria-current="page" class="sidebar-link">python环境搭建</a></li><li><a href="/share/docs/python/sql.html" class="sidebar-link">原生sql操作封装</a></li><li><a href="/share/docs/python/bug.html" class="sidebar-link">python常见问题</a></li><li><a href="/share/docs/python/regular.html" class="sidebar-link">正则表达式</a></li><li><a href="/share/docs/python/string.html" class="sidebar-link">python常用函数</a></li><li><a href="/share/docs/python/flask.html" aria-current="page" class="active sidebar-link">Flask</a></li><li><a href="/share/docs/python/file.html" class="sidebar-link">文件处理</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Flask</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><!---->
            
          <span data-v-25ba6db2>2018 - </span>
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">Flask</h1> <div data-v-f875f3fc><!----> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022/5/29</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>python</span><span class="tag-item" data-v-f875f3fc>Flask</span></i></div></div> <div class="theme-reco-content content__default"><div id="boxx" data-v-f4ca0dac><div data-v-f4ca0dac><p v-if="true" class="custom-block-title" data-v-f4ca0dac></p> <p v-if="true" data-v-f4ca0dac></p></div></div> <h2 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h2> <blockquote><p>在python虚拟环境中执行下面命令, 安装flask</p></blockquote> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>pip <span class="token function">install</span> flask
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="一个最小的应用"><a href="#一个最小的应用" class="header-anchor">#</a> 一个最小的应用</h2> <p>一个最小的 Flask 应用看起来会是这样:</p> <p>文件名称 <code>test.py</code></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">hello_world</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'Hello World!'</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    app<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><div class="custom-block danger"><p class="title">注意</p><p>确保你的应用文件名不是 flask.py ，因为这将与 Flask 本身冲突。</p></div><p>执行命令, 启动服务</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>(venv) F:\flask_env&gt;python test.py
 * Running on http://127.0.0.1:5000/
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>现在访问 <a href="http://127.0.0.1:5000/" target="_blank" rel="noopener noreferrer">http://127.0.0.1:5000/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> ，你会看见 Hello World 问候。
</p><hr><p></p> <p>那么，这段代码做了什么？</p> <ul><li>首先，我们导入了 Flask 类。这个类的实例将会是我们的 WSGI 应用程序。</li> <li>接下来，我们创建一个该类的实例，第一个参数是应用模块或者包的名称。 如果你使用单一的模块（如本例），你应该使用 <strong>name</strong> ，因为模块的名称将会因其作为单独应用启动还是作为模块导入而有不同（ 也即是 '<strong>main</strong>' 或实际的导入名）。这是必须的，这样 Flask 才知道到哪去找模板、静态文件等等。</li> <li>然后，我们使用 route() 装饰器告诉 Flask 什么样的URL 能触发我们的函数。</li> <li>这个函数的名字也在生成 URL 时被特定的函数采用，这个函数返回我们想要显示在用户浏览器中的信息。</li> <li>最后我们用 run() 函数来让应用运行在本地服务器上。 其中 if <strong>name</strong> =='<strong>main</strong>': 确保服务器只会在该脚本被 Python 解释器直接执行的时候才会运行，而不是作为模块导入的时候。</li></ul> <p>欲关闭服务器，按 Ctrl+C。
</p><hr>
外部可访问的服务器<p></p> <p>如果你运行了这个服务器，你会发现它只能从你自己的计算机上访问，网络中其它任何的地方都不能访问。在调试模式下，用户可以在你的计算机上执行任意 <code>Python</code> 代码。因此，这个行为是默认的。</p> <p>如果你禁用了 debug 或信任你所在网络的用户，你可以简单修改调用 run() 的方法使你的服务器公开可用，如下:</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>app<span class="token punctuation">.</span>run<span class="token punctuation">(</span>host<span class="token operator">=</span><span class="token string">'0.0.0.0'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这会让操作系统监听所有公网 IP。</p> <h2 id="调试模式"><a href="#调试模式" class="header-anchor">#</a> 调试模式</h2> <p>虽然 run() 方法适用于启动本地的开发服务器，但是你每次修改代码后都要手动重启它。这样并不够优雅，而且 Flask 可以做到更好。<span style="color:blue;font-weight:bold;">如果你启用了调试支持，服务器会在代码修改后自动重新载入，并在发生错误时提供一个相当有用的调试器</span>。</p> <p><span style="color:red;font-weight:bold;">有两种途径来启用调试模式。一种是直接在应用对象上设置:</span></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>app.debug <span class="token operator">=</span> True
app.run<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><span style="color:red;font-weight:bold;">另一种是作为 run 方法的一个参数传入:</span></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>app.run<span class="token punctuation">(</span>debug<span class="token operator">=</span>True<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>两种方法的效果完全相同。</p> <div class="custom-block danger"><p class="title">注意</p><ol><li>尽管交互式调试器在允许 fork 的环境中无法正常使用（也即在生产服务器上正常使用几乎是不可能的），但它依然允许执行任意代码。这使它成为一个巨大的安全隐患，因此它<span style="color:red;font-weight:bold;"> 绝对不能用于生产环境</span>。</li> <li>Flask虽然自带Web服务器，但是该服务器性能较低，是单进程单线程模型，原本是供开发测试使用。所以我们在生产环境中需要使用<span style="color:red;font-weight:bold;"> Gunicorn </span>这样高性能服务器部署Flask服务;</li></ol></div><h2 id="路由"><a href="#路由" class="header-anchor">#</a> 路由</h2> <blockquote><p>现代 Web 应用的 URL 十分优雅，易于人们辨识记忆，这一点对于那些面向使用低速网络连接移动设备访问的应用特别有用。如果可以不访问索引页，而是直接访问想要的那个页面，他们多半会笑逐颜开而再度光顾。</p></blockquote> <p>如上所见， route() 装饰器把一个函数绑定到对应的 URL 上。</p> <p>这里是一些基本的例子:</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'Index Page'</span>

<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/hello'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'Hello World'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>但是，不仅如此！你可以构造含有动态部分的 URL，也可以在一个函数上附着多个规则。</p> <h3 id="变量规则"><a href="#变量规则" class="header-anchor">#</a> 变量规则</h3> <p>要给 URL 添加变量部分，你可以把这些特殊的字段标记为 &lt;variable_name&gt; ， 这个部分将会作为命名参数传递到你的函数。规则可以用 <a href="converter:variable_name">converter:variable_name</a> 指定一个可选的转换器。这里有一些不错的例子:</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/user/&lt;username&gt;'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">show_user_profile</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># show the user profile for that user</span>
    <span class="token keyword">return</span> <span class="token string">'User %s'</span> <span class="token operator">%</span> username

<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/post/&lt;int:post_id&gt;'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">show_post</span><span class="token punctuation">(</span>post_id<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># show the post with the given id, the id is an integer</span>
    <span class="token keyword">return</span> <span class="token string">'Post %d'</span> <span class="token operator">%</span> post_id
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><hr>
转换器有下面几种：
<div><table><tr><td>int</td> <td>接受整数</td></tr> <tr><td>float</td> <td>同 int ，但是接受浮点数</td></tr> <tr><td>path</td> <td>和默认的相似，但也接受斜线</td></tr></table></div> <hr> <p><span style="color:red;font-weight:bold;">唯一 URL / 重定向行为</span></p> <p>Flask 的 URL 规则基于 Werkzeug 的路由模块。这个模块背后的思想是基于 Apache 以及更早的 HTTP 服务器主张的先例，保证优雅且唯一的 URL。</p> <p>以这两个规则为例:</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/projects/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">projects</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'The project page'</span>

<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/about'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">about</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'The about page'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>虽然它们看起来着实相似，但它们结尾斜线的使用在 URL 定义 中不同。 第一种情况中，指向 projects 的规范 URL 尾端有一个斜线。这种感觉很像在文件系统中的文件夹。访问一个结尾不带斜线的 URL 会被 Flask 重定向到带斜线的规范 URL 去。</li> <li>然而，第二种情况的 URL 结尾不带斜线，类似 UNIX-like 系统下的文件的路径名。访问结尾带斜线的 URL 会产生一个 404 “Not Found” 错误。</li> <li>这个行为使得在遗忘尾斜线时，允许关联的 URL 接任工作，与 Apache 和其它的服务器的行为并无二异。此外，也保证了 URL 的唯一，有助于避免搜索引擎索引同一个页面两次。</li></ul> <div class="custom-block danger"><p class="title">白话区别</p><ol><li><code>/projects/</code>, 可以直接访问<code>/projects/</code>,如果访问<code>/projects</code>, 会自动重定向到<code>/projects/</code>;</li> <li><code>/about</code>, 可以直接访问<code>/about</code>, 如果访问<code>/about/</code>,会报404 “Not Found”错误;</li></ol></div><h3 id="构造-url"><a href="#构造-url" class="header-anchor">#</a> 构造 URL</h3> <p>如果 Flask 能匹配 URL，那么 Flask 可以生成它们吗？</p> <p>当然可以。你可以用 url_for()来给指定的函数构造 URL。它<span style="color:red;font-weight:bold;">接受函数名作为第一个参数，也接受对应 URL 规则的变量部分的命名参数</span>。未知变量部分会添加到 URL 末尾作为查询参数。</p> <p>这里有一些例子:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; from flask import Flask, url_for
&gt;&gt;&gt; app = Flask(__name__)
&gt;&gt;&gt; @app.route('/')
... def index(): pass
...
&gt;&gt;&gt; @app.route('/login')
... def login(): pass
...
&gt;&gt;&gt; @app.route('/user/&amp;lt;username&gt;')
... def profile(username): pass
...
&gt;&gt;&gt; with app.test_request_context():
...  print url_for('index')
...  print url_for('login')
...  print url_for('login', next='/')
...  print url_for('profile', username='John Doe')
...
/
/login
/login?next=/
/user/John%20Doe
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>这里也用到了 test_request_context() 方法，下面会解释。即使我们正在通过 Python 的 shell 进行交互，它依然会告诉 Flask 要表现为正在处理一个请求。</p> <p><span style="color:red;font-weight:bold;">为什么你要构建 URL 而非在模板中硬编码？这里有三个绝妙的理由：</span></p> <ol><li>反向构建通常比硬编码的描述性更好。更重要的是，它允许你一次性修改 URL， 而不是到处边找边改。</li> <li>URL 构建会转义特殊字符和 Unicode 数据，免去你很多麻烦。</li> <li>如果你的应用不位于 URL 的根路径（比如，在 /app 下，而不是 / ），url_for() 会妥善处理这个问题。</li></ol> <h3 id="http-方法"><a href="#http-方法" class="header-anchor">#</a> HTTP 方法</h3> <p>HTTP （与 Web 应用会话的协议）有许多不同的访问 URL 方法。<span style="color:red;font-weight:bold;">默认情况下，路由只回应 GET 请求</span>，但是通过 route() 装饰器传递 methods 参数可以改变这个行为。这里有一些例子:</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> request<span class="token punctuation">.</span>method <span class="token operator">==</span> <span class="token string">'POST'</span><span class="token punctuation">:</span>
        do_the_login<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        show_the_login_form<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>如果存在 GET ，那么也会替你自动地添加 HEAD，无需干预。它会确保遵照 HTTP RFC（描述 HTTP 协议的文档）处理 HEAD 请求，所以你可以完全忽略这部分的 HTTP 规范。同样，自从 Flask 0.6 起， 也实现了 OPTIONS 的自动处理。</p> <p>你不知道一个 HTTP 方法是什么？不必担心，这里会简要介绍 HTTP 方法和它们为什么重要：</p> <p>HTTP 方法（也经常被叫做“谓词”）告知服务器，客户端想对请求的页面 做 些什么。下面的都是非常常见的方法：</p> <ol><li>GET 浏览器告知服务器：只  获取 页面上的信息并发给我。这是最常用的方法。</li> <li>POST 浏览器告诉服务器：想在 URL 上  发布 新信息。并且，服务器必须确保 数据已存储且仅存储一次。这是 HTML 表单通常发送数据到服务器的方法。</li> <li>PUT  类似POST,但是服务器可能触发了存储过程多次，多次覆盖掉旧值。你可能会问这有什么用，当然这是有原因的。考虑到传输中连接可能会丢失，在这种情况下浏览器和服务器之间的系统可能安全地第二次接收请求，而不破坏其它东西。因为POST它只触发一次，所以用POST是不可能的。</li> <li>DELETE  删除给定位置的信息。</li></ol> <h2 id="flask路由访问404-405"><a href="#flask路由访问404-405" class="header-anchor">#</a> Flask路由访问404/405</h2> <blockquote><p>路由找不到的情况, 报错404; 路由请求类型错误, 报错405, 以api的形式返回给前端, 提示更友好!</p></blockquote> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">page_not_found</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">404</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'The requested URL was not found on the server'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">405</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">page_not_found</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">405</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'The method is not allowed for the requested URL'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="蓝图"><a href="#蓝图" class="header-anchor">#</a> 蓝图</h2> <div class="custom-block danger"><p class="title">白话作用</p><p>项目模块化,   路由分组</p></div><h3 id="蓝图的作用"><a href="#蓝图的作用" class="header-anchor">#</a> 蓝图的作用</h3> <ul><li>将不同的功能模块化
<ul><li>一个项目可以具有多个Blueprint</li> <li>可以将一个Blueprint注册到任何一个未使用的URL下比如 “/”、“/sample”或者子域名</li> <li>在一个应用中，一个模块可以注册多次</li> <li>Blueprint可以单独具有自己的模板、静态文件或者其它的通用操作方法，它并不是必须要实现应用的视图和函数的</li> <li>在一个应用初始化时，就应该要注册需要使用的Blueprint</li></ul></li> <li>构建大型应用</li> <li>优化项目结构</li> <li>增强可读性、易于维护</li></ul> <h3 id="蓝图运行机制"><a href="#蓝图运行机制" class="header-anchor">#</a> 蓝图运行机制</h3> <ul><li>蓝图是保存了一组将来可以在应用对象上执行的操作，注册路由就是一种操作</li> <li>当在app对象上调用 route 装饰器注册路由时,这个操作将修改对象的url_map路由表</li> <li>然而，蓝图对象根本没有路由表，当我们在蓝图对象上调用route装饰器注册路由时,它只是在内部的一个延迟操作记录列表defered_functions中添加了一个项</li> <li>当执行app对象的 register_blueprint() 方法时，应用对象将从蓝图对象的 defered_functions 列表中取出每一项，并以自身作为参数执行该匿名函数，即调用应用对象的 add_url_rule() 方法，这将真正的修改应用对象的usr_map路由表</li></ul> <h3 id="使用蓝图的步骤"><a href="#使用蓝图的步骤" class="header-anchor">#</a> 使用蓝图的步骤</h3> <p><span style="color:blue;font-weight:bold;">  项目大体结构  </span></p> <img src="/share/python/29.png" alt="foo"> <p><span style="color:blue;font-weight:bold;">  步骤一: 修改启动文件<code>start.py</code></span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token triple-quoted-string string">&quot;&quot;&quot;
coding:utf-8
@describe:项目入口文件
@Time:2022/5/28 21:38
@Author:川
&quot;&quot;&quot;</span>
<span class="token keyword">from</span> apps <span class="token keyword">import</span> create_app

app <span class="token operator">=</span> create_app<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
app<span class="token punctuation">.</span>debug <span class="token operator">=</span> <span class="token boolean">True</span>
app<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><hr style="background-color:red;height:2px;border:none;"> <p><span style="color:blue;font-weight:bold;">  步骤二: 创建<code>apps/__init__.py</code>文件  </span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token triple-quoted-string string">&quot;&quot;&quot;
coding:utf-8
@describe:项目注册配置文件
@Time:2022/5/28 21:38
@Author:川
&quot;&quot;&quot;</span>
<span class="token keyword">import</span> os
<span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> jsonify
<span class="token keyword">from</span> apps<span class="token punctuation">.</span>views <span class="token keyword">import</span> init_view


<span class="token keyword">def</span> <span class="token function">create_app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>abspath<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>dirname<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;..&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span>
    <span class="token keyword">def</span> <span class="token function">page_not_found</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">404</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'The requested URL was not found on the server'</span><span class="token punctuation">)</span>

    <span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">405</span><span class="token punctuation">)</span>
    <span class="token keyword">def</span> <span class="token function">page_not_found</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">405</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'The method is not allowed for the requested URL'</span><span class="token punctuation">)</span>

    app<span class="token punctuation">.</span>debug <span class="token operator">=</span> <span class="token boolean">True</span>

    <span class="token comment"># 注册路由</span>
    init_view<span class="token punctuation">(</span>app<span class="token punctuation">)</span>
    <span class="token keyword">return</span> app
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><hr style="background-color:red;height:2px;border:none;"> <p><span style="color:blue;font-weight:bold;">  步骤三: 创建<code>apps/views/__init__.py</code>文件  </span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token triple-quoted-string string">&quot;&quot;&quot;
coding:utf-8
@describe:注册多模块路由文件
@Time:2022/5/28 21:38
@Author:川
&quot;&quot;&quot;</span>
<span class="token keyword">from</span> apps<span class="token punctuation">.</span>views<span class="token punctuation">.</span>api <span class="token keyword">import</span> register_api_views


<span class="token keyword">def</span> <span class="token function">init_view</span><span class="token punctuation">(</span>app<span class="token punctuation">)</span><span class="token punctuation">:</span>
register_api_views<span class="token punctuation">(</span>app<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><hr style="background-color:red;height:2px;border:none;"> <p><span style="color:blue;font-weight:bold;">  步骤四: 创建<code>apps/views/api/demo.py</code>文件  </span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token triple-quoted-string string">&quot;&quot;&quot;
coding:utf-8
@describe:创建视图函数文件
@Time:2022/5/28 21:38
@Author:川
&quot;&quot;&quot;</span>
<span class="token keyword">import</span> os

<span class="token keyword">from</span> flask <span class="token keyword">import</span> Blueprint<span class="token punctuation">,</span> request<span class="token punctuation">,</span> jsonify

<span class="token comment"># 蓝图名称api_demo</span>
api_demo <span class="token operator">=</span> Blueprint<span class="token punctuation">(</span><span class="token string">'demo'</span><span class="token punctuation">,</span> __name__<span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@api_demo<span class="token punctuation">.</span>get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">hello_world</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># 函数名不要和蓝图名重复</span>
    <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'您好, 欢迎入手Flask'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@api_demo<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/upload/'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">&quot;POST&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">upload</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;接受前端传送过来的文件&quot;&quot;&quot;</span>
    fileObj <span class="token operator">=</span> request<span class="token punctuation">.</span>files<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">&quot;file&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> fileObj <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">422</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'file文件信息不能为空'</span><span class="token punctuation">)</span>
    <span class="token comment"># 直接使用文件上传对象保存</span>
    file_name <span class="token operator">=</span> fileObj<span class="token punctuation">.</span>filename
    <span class="token comment">#   确保文件扩展名是允许的</span>
    <span class="token comment">#   os.path.splitext() 函数将文件名和扩展名分开</span>
    file_ext <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>splitext<span class="token punctuation">(</span>file_name<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
    <span class="token keyword">if</span> file_ext <span class="token keyword">not</span> <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token string">'.jpg'</span><span class="token punctuation">,</span> <span class="token string">'.png'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">422</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'当前文件扩展名是:%s,只允许上传.jpg和.png结尾的文件'</span> <span class="token operator">%</span> file_ext<span class="token punctuation">)</span>
    folder <span class="token operator">=</span> <span class="token string">'./test/test/'</span>
    os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>folder<span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token number">0o755</span><span class="token punctuation">,</span> exist_ok<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
    path <span class="token operator">=</span> <span class="token string">&quot;%s%s&quot;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>folder<span class="token punctuation">,</span> file_name<span class="token punctuation">)</span>
    fileObj<span class="token punctuation">.</span>save<span class="token punctuation">(</span>path<span class="token punctuation">)</span>
    <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'上传成功'</span><span class="token punctuation">,</span> data<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;path&quot;</span><span class="token punctuation">:</span> path<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><hr style="background-color:red;height:2px;border:none;"> <p><span style="color:blue;font-weight:bold;">  步骤五: 注册蓝图<code>apps/views/api/__init__.py</code></span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token triple-quoted-string string">&quot;&quot;&quot;
coding:utf-8
@describe:注册当前api模块下蓝图, 就是把视图函数文件都注册到这里面
@Time:2022/5/28 21:38
@Author:川
&quot;&quot;&quot;</span>
<span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask
<span class="token keyword">from</span> apps<span class="token punctuation">.</span>views<span class="token punctuation">.</span>api<span class="token punctuation">.</span>demo <span class="token keyword">import</span> api_demo


<span class="token keyword">def</span> <span class="token function">register_api_views</span><span class="token punctuation">(</span>app<span class="token punctuation">:</span> Flask<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
注册蓝图
:param app:
:return:
&quot;&quot;&quot;</span>
app<span class="token punctuation">.</span>register_blueprint<span class="token punctuation">(</span>api_demo<span class="token punctuation">,</span> url_prefix<span class="token operator">=</span><span class="token string">&quot;/api/demo&quot;</span><span class="token punctuation">)</span> <span class="token comment"># # 蓝图名称api_demo,路由访问前缀/api/demo</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><hr style="background-color:red;height:2px;border:none;"> <p><span style="color:blue;font-weight:bold;">  访问demo <code>/</code></span></p> <img src="/share/python/30.png" alt="foo"> <p><span style="color:blue;font-weight:bold;">  访问demo <code>/upload/</code></span></p> <img src="/share/python/31.png" alt="foo"> <h2 id="flask配置文件"><a href="#flask配置文件" class="header-anchor">#</a> flask配置文件</h2> <blockquote><p>在一些项目中，处于安全性的要求，一般不将密码，key等放入到配置文件中。然而这些代码又是上传在git等平台上。为了方便管理。一般采用系统变量的方式来实现。</p></blockquote> <div class="custom-block danger"><p class="title">注意</p><p>修改<code>.flaskenv</code>或<code>.env</code>之后, 需要重启项目,配置才会生效!!</p></div><p>flask官方推荐使用python-dotenv包来管理特殊的配置。</p> <p>下载安装:</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>pip <span class="token function">install</span> python-dotenv
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>在项目根目录创建<code>.flaskenv</code>文件用来存储和Flask相关的环境变量，也可以创建<code>.env</code>文件用来存储包含敏感信息的环境变量。</p> <p>但<code>.env</code> 文件的优先级高于<code>.flaskenv</code>这个文件, 如果两个文件存在同样名称的key, 会直接使用<code>.env</code>文件的环境变量key</p> <p>文件如下:</p> <div class="language-env line-numbers-mode"><pre class="language-text"><code>MYSQL_USER=&quot;root&quot;
MYSQL_PASSWORD=&quot;mysql&quot;
MYSQL_HOST=&quot;localhost&quot;
MYSQL_PORT=3306
MYSQL_DATABASE=&quot;test&quot;

REDIS_HOST=&quot;localhost&quot;
REDIS_PORT=6379
REDIS_DECODE_RESPONSES=true
REDIS_PASSWORD=&quot;&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><span style="color:red;font-weight:bold;">使用方式: </span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token comment"># 获取env或者flaskenv中的MYSQL_USER环境变量, 获取不到默认为root</span>
mysql_user <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">&quot;MYSQL_USER&quot;</span><span class="token punctuation">,</span>root<span class="token punctuation">)</span>
mysql_user <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">&quot;MYSQL_USER&quot;</span><span class="token punctuation">)</span> <span class="token keyword">or</span> root
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><span style="color:red;font-weight:bold;">flask项目中使用方式: </span></p> <img src="/share/python/32.png" alt="foo"> <p><span style="color:red;font-weight:bold;">引入settings.py文件变量的方法就是普通的python导入 </span></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>from settings <span class="token function">import</span> MYSQL_USER
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><img src="/share/python/33.png" alt="foo"> <h2 id="flask日志"><a href="#flask日志" class="header-anchor">#</a> flask日志</h2> <blockquote><p>自带logging模块日志, 日志是bug处理, 重要信息备份, 必备的处理手段</p></blockquote> <h3 id="日志级别"><a href="#日志级别" class="header-anchor">#</a> 日志级别</h3> <div class="custom-block danger"><p class="title">注意</p><ol><li>开发应用程序或部署开发环境时，可以使用 DEBUG 或 INFO 级别的日志获取尽可能详细的日志信息来进行开发或部署调</li> <li>应用上线或部署生产环境时，应该使用 WARNING 或 ERROR 或 CRITICAL 级别的日志来降低机器的I/O压力和提高获取错误日志信息的效率。</li></ol></div><ul><li>DEBUG 最详细的日志信息，典型应用场景是 问题诊断</li> <li>INFO  信息详细程度仅次于DEBUG，通常只记录关键节点信息，用于确认一切都是按照我们预期的那样进行工作</li> <li>WARNING  当某些不期望的事情发生时记录的信息（如，磁盘可用空间较低），但是此时应用程序还是正常运行的</li> <li>ERROR 由于一个更严重的问题导致某些功能不能正常运行时记录的信息</li> <li>FATAL/CRITICAL 整个系统即将/完全崩溃</li></ul> <h3 id="代码配置"><a href="#代码配置" class="header-anchor">#</a> 代码配置</h3> <div class="custom-block danger"><p class="title">注意</p><p>level=logging.INFO 表示级别大于等于INFO的, 才会输出到文件里面记录下来</p></div><div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token comment"># 日志目录地址</span>
log_dir_name <span class="token operator">=</span> <span class="token string">&quot;logs%s%s&quot;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>os<span class="token punctuation">.</span>sep<span class="token punctuation">,</span> datetime<span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">&quot;%Y%m&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>log_dir_name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>log_dir_name<span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token number">0o755</span><span class="token punctuation">)</span>
<span class="token comment"># 日志文件名，以日期作为文件</span>
log_file_name <span class="token operator">=</span> <span class="token string">'flask-'</span> <span class="token operator">+</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">'%m-%d'</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span>localtime<span class="token punctuation">(</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'.log'</span>
<span class="token comment"># 创建日志文件名: 目录+文件名</span>
filename <span class="token operator">=</span> log_dir_name <span class="token operator">+</span> os<span class="token punctuation">.</span>sep <span class="token operator">+</span> log_file_name
<span class="token comment"># 日志格式:打印日志的时间-打印日志级别名称-打印当前执行程序的路径-打印日志的当前函数</span>
<span class="token comment">#                  -打印日志的当前行号-打印日志信息</span>
log_format_str <span class="token operator">=</span> <span class="token string">'%(asctime)s - %(levelname)s - %(pathname)s - %(funcName)s - %(lineno)s - %(message)s'</span>
<span class="token comment"># 配置日志,配置日志格式,日志存放文件名称,日志记录等级,日志输出字符类型</span>
logging<span class="token punctuation">.</span>basicConfig<span class="token punctuation">(</span><span class="token builtin">format</span><span class="token operator">=</span>log_format_str<span class="token punctuation">,</span> filename<span class="token operator">=</span>filename<span class="token punctuation">,</span> level<span class="token operator">=</span>logging<span class="token punctuation">.</span>INFO<span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span>
<span class="token comment">#  添加日志处理器</span>
app<span class="token punctuation">.</span>logger<span class="token punctuation">.</span>addHandler<span class="token punctuation">(</span>logging<span class="token punctuation">.</span>StreamHandler<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># 注册路由</span>
init_view<span class="token punctuation">(</span>app<span class="token punctuation">)</span>
app<span class="token punctuation">.</span>debug <span class="token operator">=</span> APP_DEBUG
<span class="token keyword">return</span> app
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="日志调用"><a href="#日志调用" class="header-anchor">#</a> 日志调用</h3> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token decorator annotation punctuation">@api_demo<span class="token punctuation">.</span>get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">hello_world</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 函数名不要和蓝图名api_demo重复</span>
    <span class="token keyword">import</span> logging
    logging<span class="token punctuation">.</span>error<span class="token punctuation">(</span><span class="token string">&quot;开心就好error!&quot;</span><span class="token punctuation">)</span>
    logging<span class="token punctuation">.</span>debug<span class="token punctuation">(</span><span class="token string">&quot;debug无言!&quot;</span><span class="token punctuation">)</span>
    logging<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token string">&quot;info详情!&quot;</span><span class="token punctuation">)</span>
    logging<span class="token punctuation">.</span>warning<span class="token punctuation">(</span><span class="token string">&quot;warning开心!&quot;</span><span class="token punctuation">)</span>
    logging<span class="token punctuation">.</span>error<span class="token punctuation">(</span><span class="token string">&quot;错误提示日志&quot;</span><span class="token punctuation">)</span>
    logging<span class="token punctuation">.</span>critical<span class="token punctuation">(</span><span class="token string">&quot;123错误提示日志&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> jsonify<span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'您好, 欢迎入手Flask66'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="日志输出"><a href="#日志输出" class="header-anchor">#</a> 日志输出</h3> <div class="language-text line-numbers-mode"><pre class="language-text"><code>2022-06-01 16:09:57,655 - ERROR - F:\flask_env\apps\views\api\demo.py - hello_world - 20 - 开心就好error!
2022-06-01 16:09:57,655 - INFO - F:\flask_env\apps\views\api\demo.py - hello_world - 22 - info详情!
2022-06-01 16:09:57,655 - WARNING - F:\flask_env\apps\views\api\demo.py - hello_world - 23 - warning开心!
2022-06-01 16:09:57,656 - ERROR - F:\flask_env\apps\views\api\demo.py - hello_world - 24 - 错误提示日志
2022-06-01 16:09:57,656 - CRITICAL - F:\flask_env\apps\views\api\demo.py - hello_world - 25 - 123错误提示日志
2022-06-01 16:09:57,656 - INFO - F:\flask_env\venv\lib\site-packages\werkzeug\_internal.py - _log - 224 - 
127.0.0.1 - - [01/Jun/2022 16:09:57] &quot;GET /api/demo/ HTTP/1.1&quot; 200 -
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/6/1 16:23:27</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/share/docs/python/string.html" class="prev">
            python常用函数
          </a></span> <span class="next"><a href="/share/docs/python/file.html">
            文件处理
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#安装" class="sidebar-link reco-side-安装" data-v-cb1513f6>安装</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#一个最小的应用" class="sidebar-link reco-side-一个最小的应用" data-v-cb1513f6>一个最小的应用</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#调试模式" class="sidebar-link reco-side-调试模式" data-v-cb1513f6>调试模式</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#路由" class="sidebar-link reco-side-路由" data-v-cb1513f6>路由</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#变量规则" class="sidebar-link reco-side-变量规则" data-v-cb1513f6>变量规则</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#构造-url" class="sidebar-link reco-side-构造-url" data-v-cb1513f6>构造 URL</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#http-方法" class="sidebar-link reco-side-http-方法" data-v-cb1513f6>HTTP 方法</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#flask路由访问404-405" class="sidebar-link reco-side-flask路由访问404-405" data-v-cb1513f6>Flask路由访问404/405</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#蓝图" class="sidebar-link reco-side-蓝图" data-v-cb1513f6>蓝图</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#蓝图的作用" class="sidebar-link reco-side-蓝图的作用" data-v-cb1513f6>蓝图的作用</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#蓝图运行机制" class="sidebar-link reco-side-蓝图运行机制" data-v-cb1513f6>蓝图运行机制</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#使用蓝图的步骤" class="sidebar-link reco-side-使用蓝图的步骤" data-v-cb1513f6>使用蓝图的步骤</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#flask配置文件" class="sidebar-link reco-side-flask配置文件" data-v-cb1513f6>flask配置文件</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/flask.html#flask日志" class="sidebar-link reco-side-flask日志" data-v-cb1513f6>flask日志</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#日志级别" class="sidebar-link reco-side-日志级别" data-v-cb1513f6>日志级别</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#代码配置" class="sidebar-link reco-side-代码配置" data-v-cb1513f6>代码配置</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#日志调用" class="sidebar-link reco-side-日志调用" data-v-cb1513f6>日志调用</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/flask.html#日志输出" class="sidebar-link reco-side-日志输出" data-v-cb1513f6>日志输出</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="left:68px;bottom:370px;display:none;" data-v-5775ee02>
      欢迎来到我的博客
    </div> <div class="operation" style="right:90px;bottom:40px;display:none;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="260px" height="420" class="live2d" style="right:200px;bottom:20px;opacity:0.4;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><!----><div class="RibbonAnimation"></div><!----></div></div>
    <script src="/share/assets/js/app.706684de.js" defer></script><script src="/share/assets/js/3.4685d505.js" defer></script><script src="/share/assets/js/1.b12738fb.js" defer></script><script src="/share/assets/js/67.fd9ba6df.js" defer></script><script src="/share/assets/js/9.4e81f08e.js" defer></script>
  </body>
</html>
